/**
 *  版权所有 Copyright@2016-3-28下午6:18:34
 */
package com.fairy.sso.core;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * [NetHelp.java.java]
 *
 * @Author: [yyk1504@163.com]
 * @CreateDate: [2016-3-28下午6:18:34]
 * @Update: [说明本次修改内容] by [yyk1504@163.com] [2016-3-28下午6:18:34]
 * @Version: [v1.0]
 */
public class NetHelp {

	/**
	 * <p>获取域名，如：http://f0rb.iteye.com/ </p>
	 * @param request
	 * @return
	 */
	public static String getDomain(HttpServletRequest request)
	{
		StringBuffer url = request.getRequestURL();  
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
		
		return tempContextUrl;
	}
	public static String getDomain(String  url)
	{
		String str="";
		url=url.toLowerCase();
		url=url.replace("http://", "");
		int index=url.indexOf("/");
		if(index!=-1)
		{
			str=url.substring(0, index);
			int index1=str.lastIndexOf(":");
			str=str.substring(0, index1);
		}

		return str;
	}
	/**
	 * <p>获取带部署环境上下文的域名，如： http://www.iteye.com/admin/ </p>
	 * @param request
	 * @return
	 */
	public static String getDomainName(HttpServletRequest request)
	{
		StringBuffer url = request.getRequestURL();  
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).append("/").toString(); 
		return tempContextUrl;
	}
	/**
	 * <p>获取带部署环境上下文的域名，如： http://www.iteye.com/admin/ </p>
	 * @param request
	 * @return
	 */
	public static String getAgent(HttpServletRequest request)
	{
		String agent = request.getHeader("user-agent");

		return agent;
	}

	/**
	 * <p>获得 请求的header信息 </p>
	 * @param request
	 * @return
	 */
	public static Map<String, String> getHeadersInfo(HttpServletRequest request) 
	{
	    Map<String, String> map = new HashMap<String, String>();
	    Enumeration<String> headerNames = request.getHeaderNames();
	    while (headerNames.hasMoreElements()) {
	        String key = headerNames.nextElement();
	        String value = request.getHeader(key);
	        map.put(key, value);
	    }
	    return map;
	}
	public static void main(String[] args)
	{
		String str="http://www.iteye.com/admin/";
		System.out.println(getDomain(str));
	}
}
